package com.mes.utils.zebra;

public class StringConvertUtils {
	
	 private static int HYC_CHAR_BYTE_LENGTH = 6;
	 private static int HYC_DIGIT_BYTE_LENGTH = 4;
	 public static char[] CHARS = new char[]{
	            'u', 'q', 'h', 'M', 'b', 'v', 'j', 'w', 'H', 'W', 'O', 'z', 'l', 't',
	            'A', 'P', 'y', 'a', 'K', 'B', 'e', 's', 'n', 'C', 'I', 'x', '2', 'c',
	            'D', 'N', 'g', '6', 'i', 'Q', 'Z', '0', 'm', 'G', 'V', 'd', '4', 'U',
	            'T', 'E', '3', 'r', 'L', 'p', 'F', '9', 'S', 'Y', '7', 'f', 'X', '1',
	            'k', 'R', '8', 'J', 'o', '5'
	    };
	    public static char[] DIGIT_CHARS = new char[]{
	            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
	    };
	    
	    /*
	     * 二进制字符串转换成16进制字符串
	     * @param bitString
	     * @return String
	     * 
	     */
	public static String bitStringToHexString(String bitString) {
        StringBuffer sb = new StringBuffer();
        int blength = bitString.length() % 4;
        if(blength!=0){
            String first = bitString.substring(0,blength);
            sb.append(Integer.toHexString(Integer.parseInt(first,2)));
            bitString = bitString.substring(blength);
        }
        int cnum = bitString.length() / 4;
        for(int i=0;i<cnum;i++){
            sb.append(Integer.toHexString(Integer.parseInt(bitString.substring(i*4,4*(i+1)),2)));
        }
        return sb.toString().toUpperCase();
    }
	 /*
     * 字符串转换成二进制字符串
     * @param srcStr
     * @return String
     * 
     */
    public static String stringToBitString(String srcStr)
    {
        String str1 = "";
        if (srcStr == null || srcStr.equals("")) return null;
        System.out.println("--------length="+srcStr.length() );
        System.out.println("--------srcStr="+srcStr );
//        if (srcStr.length() != 24) 
//        {
//        	throw new IllegalArgumentException("this string must 24 length");
//        }
        //15位字母部分
        str1 = srcStr.substring(0, 15); //srcStr.SubString(0, 15);
        //9位数字部分
        String str2 = srcStr.substring(15);//15, HYC_EPC_LENGTH - 1
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str1.length(); i++)
        {
            char[] charT = str1.toCharArray();
            String temp = charToFixLengBit(charT[i]);
            sb.append(temp);
        }

        for (int i = 0; i < str2.length(); i++)
        {
            char[] charT = str2.toCharArray();
            String temp = digitCharToFixLengBit(charT[i]);
            sb.append(temp);
        }
        sb.append("00");
        String bitString = sb.toString();
        return bitString;
    }
    
    
    /*
     * char转换成bitString
     * @param char
     * @return String
     * 例如 a-->000001（一个字符char用6个bit位表示）
     */
    public static String charToFixLengBit(char ch)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < CHARS.length; i++)
        {
            if (CHARS[i] == ch)
            {
                String temp = Integer.toBinaryString(i);
                int length = temp.length();
                //不足6位前面补0
                int suff = HYC_CHAR_BYTE_LENGTH - length;
                for (int j = 0; j < suff; j++)
                {
                    sb.append("0");
                }
                sb.append(temp);
                return sb.toString();
            }
        }
        return null;
    }


    /* digitchar转换成bitString
     * @param char
     * @return String
     * 例如 1-->0001（1个char数字用4个bit位表示）
     */
    public static String digitCharToFixLengBit(char ch)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < DIGIT_CHARS.length; i++)
        {
            if (DIGIT_CHARS[i] == ch)
            {
                String temp = Integer.toBinaryString(i);
                int length = temp.length();
                //不足4位前面补0
                int suff = HYC_DIGIT_BYTE_LENGTH - length;
                for (int j = 0; j < suff; j++)
                {
                    sb.append("0");
                }
                sb.append(temp);
                return sb.toString();
            }
        }
        return null;
    }
    
    /*
     * 16进制字符串转换成byte数组
     * @param srcStr
     * @return String
     * 
     */
    public static byte[] hexStringToByteArray(String hexString) {
        if (isEmpty(hexString))
            throw new IllegalArgumentException("this hexString must not be empty");

        hexString = hexString.toLowerCase();
        final byte[] byteArray = new byte[hexString.length() / 2];
        int k = 0;
        for (int i = 0; i < byteArray.length; i++) {//因为是16进制，最多只会占用4位，转换成字节需要两个16进制的字符，高位在先
            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
            byteArray[i] = (byte) (high << 4 | low);
            k += 2;
        }
        return byteArray;
    }
    
  
    
    
    public static boolean isEmpty(String str) {
    	return str ==null || str.length()==0;
    }
    
    /*
     * 字符串转换成byte数组
     * @param srcStr
     * @return String
     * 
     */
    public static byte[] stringToBytes(String str) {
    	System.out.println("--------str="+str);
    	String bitString = stringToBitString(str);
    	System.out.println("--------bitString="+bitString);
    	String hexString = bitStringToHexString(bitString);
    	System.out.println("--------hexString="+hexString);
    	byte[] result = hexStringToByteArray(hexString);
    	return result;
    }
    
    /*
     * 16进制字符串转换成byte数组
     * @param srcStr
     * @return String
     * 
     */
    public static byte[] hexStringToBytes(String hexString) {
    	hexString = "2280" + hexString + "09BB";
    	byte[] result = hexStringToByteArray(hexString);
    	return result;
    }
    
    public static byte[] bitStringToBytes(String bitString) {
    	
    	String hexString = bitStringToHexString(bitString);
    	System.out.println("--------hexString="+hexString);
    	byte[] result = hexStringToByteArray(hexString);
    	return result;
    }
    
    public static String stringToAscii(String value) {
        StringBuffer sbu = new StringBuffer();
        char[] chars = value.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i != chars.length - 1) {
                sbu.append((int) chars[i]).append(",");
            } else {
                sbu.append((int) chars[i]);
            }
        }
        return sbu.toString();
    }


    public static String asciiToString(String value) {
    	
        StringBuffer sbu = new StringBuffer();
        String[] chars = value.split(",");
        for (int i = 0; i < chars.length; i++) {
        	
            sbu.append((char) Integer.parseInt(chars[i]));
        }
        return sbu.toString();
    }
    
    public static String hexStringToAscii(String value) {
    	
    	 StringBuffer sbu = new StringBuffer();
	        int cnum = value.length() / 2;
	        for(int i=0;i<cnum;i++){
	        	 if (i != cnum - 1) {
	        		 sbu.append(value.substring(i*2,2*(i+1))).append(",");
	             } else {
	            	 sbu.append(value.substring(i*2,2*(i+1)));
	             }
	        	
	        }
	        
	        return sbu.toString();
     }
    
   public static String hexStringAsciiToString(String value) {
    	
	   if(value.length() % 2==0) {
	        StringBuffer sbu = new StringBuffer();
	        int cnum = value.length() / 2;
	        for(int i=0;i<cnum;i++){
	        	sbu.append((char) Integer.parseInt(value.substring(i*2,2*(i+1))));
	        }
	        
	        return sbu.toString();
	   }
	   return value;
    }
  
}
